package com.tengju.bff.interfaces.manage.crm.motivation;

import com.tengju.bff.interfaces.covercharge.util.CsvExportUtil;
import com.tengju.bff.interfaces.manage.crm.motivation.facade.MotivationActivityConfigFacade;
import com.tengju.bff.interfaces.manage.crm.motivation.facade.MotivationPerformanceObjectiveFacade;
import com.tengju.bff.interfaces.manage.crm.motivation.form.AddMotivationActivityConfigForm;
import com.tengju.bff.interfaces.manage.crm.motivation.form.GetActivityPerformanceObjectiveListForm;
import com.tengju.bff.interfaces.manage.crm.motivation.form.GetMotivationActivityConfigForm;
import com.tengju.bff.interfaces.manage.crm.motivation.form.RemoveMotivationActivityConfigForm;
import com.tengju.bff.interfaces.manage.crm.motivation.vo.MotivationActivityConfigDetailVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.motivation.application.activity.MotivationActivityConfigService;
import com.tengju.motivation.application.shared.ExcelAnalysisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author: 李大仙
 * @date: 2021/7/22 2:57 下午
 */
@Validated
@RestController
@Api(tags = "激励活动管理")
@RequestMapping(value = "manage/crm/motivation/activity/config")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MotivationActivityConfigController {

    private final MotivationActivityConfigFacade motivationActivityConfigFacade;
    private final MotivationPerformanceObjectiveFacade performanceObjectiveFacade;
    private final MotivationActivityConfigService motivationActivityConfigService;

    @ApiOperation(value = "分页查询激励活动管理列表", notes = "分页查询激励活动管理列表")
    @RequestMapping(value = "getByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> getByPage(@Valid GetMotivationActivityConfigForm form) {
        return ApiResponse.newSuccess(motivationActivityConfigFacade.getByPage(form));
    }

    @ApiOperation(value = "分页查询活动绩效目标列表", notes = "分页查询活动绩效目标列表")
    @RequestMapping(value = "getPerformanceObjectiveListByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> getPerformanceObjectiveListByPage(@Valid GetActivityPerformanceObjectiveListForm form) {
        return ApiResponse.newSuccess(performanceObjectiveFacade.getPerformanceObjectiveListByPage(form));
    }

    @ApiOperation(value = "获取活动目标类型列表", notes = "获取活动目标类型列表")
    @GetMapping(value = "getObjectTypeList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getObjectTypeList() {
        return ApiResponse.newSuccess(motivationActivityConfigFacade.getObjectTypeList());
    }

    @ApiOperation(value = "保存激励活动管理", notes = "保存激励活动管理")
    @RequestMapping(value = "save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Integer> save(@Valid @RequestBody AddMotivationActivityConfigForm form) {
        String staffName = ServletContextUtil.getServletContext().getRealName();
        return ApiResponse.newSuccess(motivationActivityConfigService.save(form.getActivityId(),
                form.getTitle(),
                form.getObjectType(),
                form.getRemark(),
                form.getStartTime(),
                form.getEndTime(),
                staffName));
    }

    @ApiOperation(value = "查询激励活动明细", notes = "查询激励活动明细")
    @RequestMapping(value = "detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<MotivationActivityConfigDetailVO> detail(@NotNull(message = "活动Id不能为空") Long activityId) {
        return ApiResponse.newSuccess(motivationActivityConfigFacade.detail(activityId));
    }

    @ApiOperation(value = "删除激励活动管理", notes = "删除激励活动管理")
    @RequestMapping(value = "remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Integer> remove(@Valid @RequestBody RemoveMotivationActivityConfigForm form) {
        return ApiResponse.newSuccess(motivationActivityConfigService.remove(form.getActivityId()));
    }

    @ApiOperation(value = "获取激励活动绩效目标模版文件下载地址", notes = "获取激励活动绩效目标模版文件下载文件")
    @RequestMapping(value = "/getTemplate", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<String> getActivityPerformanceObjectiveTemplate() {
        return ApiResponse.newSuccess(motivationActivityConfigService.getActivityPerformanceObjectiveTemplate());
    }

    @ApiOperation(value = "导入活动绩效目标", notes = "导入活动绩效目标")
    @RequestMapping(value = "/import", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> importPerformanceObjective(@RequestParam("file") MultipartFile multipartFile,
                                                           @RequestParam("activityId") Long activityId) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        String operatorName = servletContext.getRealName();
        List<String[]> rows = ExcelAnalysisUtil.analysis(multipartFile);
//        motivationActivityConfigService.importPerformanceObjective(rows, activityId, operatorName);
        return ApiResponse.newSuccess();
    }

    @ApiOperation(value = "获取导入说明", notes = "获取导入说明")
    @RequestMapping(value = "/getImportDesc", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<String>> getImportDesc() {
        return ApiResponse.newSuccess(motivationActivityConfigFacade.getImportDesc());
    }

    @ApiOperation(value = "导出活动绩效目标", notes = "导出活动绩效目标")
    @RequestMapping(value = "/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Object> export(GetActivityPerformanceObjectiveListForm form) throws Exception {
        form.setPageIndex(1);
        form.setPageSize(50000);
        TableVO tableVO = motivationActivityConfigFacade.exportActivityPerformanceObjective(form);
        HttpServletRequest request = ServletContextUtil.getRequest();
        HttpServletResponse response = ServletContextUtil.getResponse();
        String fileName = "活动绩效目标KPI";
        CsvExportUtil.responseSetProperties(fileName, request, response);
        CsvExportUtil.doExport(tableVO, response.getOutputStream());
        return ApiResponse.newSuccess();
    }
}
